package com.itbaizhan.dao;

import com.itbaizhan.domain.Student;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class Studentdao {
    //查询所有学生
    public List<Student> findAll() throws Exception {
        //获得db.properties文件内容
        Properties properties = new Properties();
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("db.properties");
        properties.load(is);
        String url = properties.getProperty("url");
        String user = properties.getProperty("username");
        String pwd = properties.getProperty("pwd");

        //jdbc语句
        Connection conn = DriverManager.getConnection(url,user,pwd);
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from student");
        //  处理结果集
        List<Student> list = new ArrayList<>();
        while (resultSet.next()){

           int id = resultSet.getInt("id");
           String name = resultSet.getString("studentname");
           String sex = resultSet.getString("sex");
           String address = resultSet.getString("address");
           Student student = new Student(id,name,sex,address);
           list.add(student);
        }
//释放资源
        resultSet.close();
        statement.close();
        conn.close();
        return list;
    }
}
